﻿Imports System.Data.SqlClient
Imports System.Configuration

Imports doan_1065019_1065001.AppSettingsWriter

Public Class SqlDataProvider

    Dim w As AppSettingsWriter = New AppSettingsWriter()


    Public Shared databasename As String = AppSettingsWriter.GetValue("Databasename")
    Public Shared servername As String = AppSettingsWriter.GetValue("Servername")

    Public Shared Function ConnectionData() As SqlConnection
        ' Dim CnStr As String = "Server=" & servername & ";Database= " & databasename & ";uid=sa;pwd=123456"
        Dim CnStr As String = "Server=" & servername & ";Database= " & databasename & ";Integrated Security=SSPI"
        Dim cn As SqlConnection = New SqlConnection(CnStr)
        Return cn
    End Function

    Public Shared Sub BacKup(ByVal path As String)
        Dim sqlBackup As String = "BACKUP DATABASE [" & databasename & "] TO DISK='" + path + "'"
        Try
            Dim con As SqlConnection = SqlDataProvider.ConnectionData()

            Dim cmd As SqlCommand = New SqlCommand(sqlBackup, con)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
            MessageBox.Show("Đã backup cơ sở dữ liệu đến " + path + "")
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Backup Database loi ")
        End Try
    End Sub

    Public Shared Sub Restore(ByVal path As String)
        Dim sqlBackup As String = "Use master Restore Database [" & databasename & "] from disk='" + path + "'"
        Try
            Dim con As SqlConnection = SqlDataProvider.ConnectionData()

            Dim cmd As SqlCommand = New SqlCommand(sqlBackup, con)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
            MessageBox.Show("Đã restore cơ sở dữ liệu thành công")
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Restore Database bị lỗi ")
        End Try
    End Sub

    Public Shared Sub NonQuery(ByVal sql As String)

        Dim con As SqlConnection = SqlDataProvider.ConnectionData()

        Dim cmd As SqlCommand = New SqlCommand(sql, con)
        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()

    End Sub


End Class
